package com.ddx.controller.core;

import com.ddx.common.exception.ExceptionUtil;
import com.ddx.common.exception.ResultModel;
import com.ddx.dto.StoreDto;
import com.ddx.pojo.ShoppingStore;
import com.ddx.service.StoreService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: liyuxiang
 * @create: 2019-01-06
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping(value = "/app/store")
public class StoreController {

    private static final Logger logger = LoggerFactory.getLogger(StoreController.class);

    @Autowired
    StoreService storeService;

    /**
     * 创建店铺
     * @param request
     * @param storeDto
     * @return
     */
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/register")
    public ResultModel register(HttpServletRequest request, @RequestBody StoreDto storeDto) {
        try {
            int userId = Integer.parseInt(request.getHeader("userId"));
            return storeService.register(userId, storeDto);
        } catch (Exception e) {
            logger.error("register fail,", e);
            return ResultModel.getErrorInstance("创建店铺失败，请稍候重试");
        }
    }

    /**
     * 更新店铺信息
     * @param request
     * @param storeDto
     * @return
     */
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/edit")
    public ResultModel edit(HttpServletRequest request, @RequestBody StoreDto storeDto) {
        try {
            int userId = Integer.parseInt(request.getHeader("userId"));
            return storeService.edit(userId, storeDto);
        } catch (Exception e) {
            logger.error("register fail,", e);
            return ResultModel.getErrorInstance("更新店铺失败，请稍候重试");
        }
    }

    /**
     * 店铺首页
     *
     * @param request
     * @return
     */
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/index")
    public ResultModel index(HttpServletRequest request) {
        ResultModel resultModel=ResultModel.getSuccessInstance();
        String userId = request.getHeader("userId");
        logger.debug("店铺首页：{}", userId);
//        ModelAndView mv = new ModelAndView("/app/order/index");
        try {
            ShoppingStore shoppingStore=storeService.index(Integer.parseInt(userId));
            Map<String,Object> map=new HashMap<>();
            map.put("storeInfo",shoppingStore);
            resultModel.setData(map);
//            int index = Integer.parseInt(String.valueOf(map.get("index")));
//            int size = Integer.parseInt(String.valueOf(map.get("size")));
        } catch (Exception e) {
            logger.error("店铺首页 fail,", e);
            resultModel= ExceptionUtil.getMessageException(e);
        }
        return resultModel;
    }
    /**
     * 店铺首页商品信息
     *
     * @param request
     * @return
     */
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/shopInfo")
    public ResultModel shopInfo(HttpServletRequest request,@RequestBody Map<String,Object> map) {
        ResultModel resultModel=ResultModel.getSuccessInstance();
        String userId = request.getHeader("userId");
        logger.debug("店铺首页商品信息：{}", userId);
//        ModelAndView mv = new ModelAndView("/app/order/index");
        try {
//            ShoppingStore shoppingStore=storeService.shopInfo(map);
            Map<String,Object> result=new HashMap<>();
//            result.put("storeInfo",shoppingStore);
            resultModel.setData(result);
//            int index = Integer.parseInt(String.valueOf(map.get("index")));
//            int size = Integer.parseInt(String.valueOf(map.get("size")));
        } catch (Exception e) {
            logger.error("店铺首页 fail,", e);
            resultModel= ExceptionUtil.getMessageException(e);
        }
        return resultModel;
    }
}
